<?php

namespace App\Modules\Common;


use App\Library\Rank\AbstractSingleRank;
use App\Modules\Order\Models\Order;
use App\Modules\Rebate\Models\Rebate;
use App\Modules\User\Models\User;
use Carbon\Carbon;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Redis;

class MoneyRank extends AbstractSingleRank
{
    protected static $key = "staff:money_ranking";

    public static function setKey($prefix)
    {
        self::$key = "staff:money_ranking:{$prefix}";
    }

    public static function increaseAll($id, $step = 1)
    {
        self::setKey('quarterly');
        self::increase($id, $step);

        self::setKey('annual');
        self::increase($id, $step);

        self::setKey('previous');
        self::increase($id, $step);

        for ($i = 2019; $i < (int)date('Y'); $i++) {
            self::setKey('previous'.$i);
            self::increase($id, $step);
        }
    }

    public static function flush()
    {
        self::setKey('quarterly');
        Redis::del(static::$key);

        self::setKey('annual');
        Redis::del(static::$key);

        self::setKey('previous');
        Redis::del(static::$key);

        for ($i = 2019; $i < (int)date('Y'); $i++) {
            self::setKey('previous'.$i);
            Redis::del(static::$key);
        }
    }

    public static function refresh()
    {
        static::flush();
        self::multi(function () {
            Order::query()
                ->join('users', 'users.id', '=', 'orders.referrer_id')
                ->where('orders.referrer_level', 3)
                ->where('orders.status', Order::STATUS_PAID)
                // ->where('orders.created_at', '>', now()->startOfYear()->toDateString())
                ->where('users.level', 3)
                ->orderBy('orders.id')
                ->select('orders.*')
                ->each(function ($item) {
                    // self::setKey('annual');
                    // self::increase($item->referrer_id, $item->original_price);
                    // if ($item->created_at > now()->startOfQuarter()->toDateTimeString()) {
                    //     self::setKey('quarterly');
                    //     self::increase($item->referrer_id, $item->original_price);
                    // }
                    if ($item->created_at > now()->startOfQuarter()->toDateTimeString()) {
                        // 季度
                        self::setKey('quarterly');
                        self::increase($item->referrer_id, $item->original_price);
                    }else if ($item->created_at > now()->startOfYear()->toDateTimeString()){
                        //年度
                        self::setKey('annual');
                        self::increase($item->referrer_id, $item->original_price);
                    }else if ($item->created_at < now()->startOfYear()->toDateTimeString()) {
                        //往年
                        self::setKey('previous');
                        self::increase($item->referrer_id, $item->original_price);

                        //年份
                        for ($i = 2019; $i < (int)date('Y'); $i++) { 
                            if (($item->created_at >= $i . '-01-01 00:00:00') && ($item->created_at <= $i . '-12-31 23:59:59')) {
                                self::setKey('previous'.$i);
                                self::increase($item->referrer_id, $item->original_price);
                            }
                        }
                    }
                    
                });
        });
    }
}
